<template>
  <!-- 资讯分类管理 -->
  <div>
    <!-- 导航栏 -->
    <div class="header">
      <!-- 添加按钮 -->
      <el-button
        type="primary"
        class="buttonStyle"
        @click="addInformation"
      >添加</el-button>
    </div>
    <!-- 表格主体 -->
    <div class="content">
      <el-table :data="informationData" style="width: 100%">
        <!-- 序号 -->
        <el-table-column type="index" label="序号" align="center" />
        <!-- 名称 -->
        <el-table-column prop="name" label="名称" align="center" />
        <!-- 排序号 -->
        <el-table-column prop="id" label="排序号" align="center" />
        <!-- 描述 -->
        <el-table-column prop="description" label="描述" align="center" />
        <el-table-column style="width:2000px" align="center" />
        <el-table-column style="width:2000px" align="center" />
        <el-table-column style="width:2000px" align="center" />
        <el-table-column style="width:2000px" align="center" />
        <el-table-column style="width:2000px" align="center" />
        <el-table-column style="width:2000px" align="center" />
        <el-table-column style="width:2000px" align="center" />
        <!-- 操作 -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope"> <!--作用域插槽 -->
            <el-button type="text" size="small" @click="editInformation(scope.row)">
              编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              style="color:red"
              @click="
                IdInformationdelete = scope.row.id;
                dialogVisible2 = true;
              "
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框保存或者更新 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" @close="dialogClose">
      <!-- 表单区域 -->
      <!-- :model 表单对象 最终我们要提交的那个表单对象 -->
      <!-- :rules 表单验证的规则 -->
      <!-- ref 类比与 id 最终通过ref找到这张表单 -->
      <el-form
        ref="information_form"
        :model="informationForm"
        :rules="rules"
        label-width="80px"
      >
        <!-- 栏目名称 -->
        <el-form-item label="栏目名称" prop="name">
          <el-input v-model="informationForm.name" />
        </el-form-item>
        <!-- 序号 -->
        <el-form-item label="排序号" prop="no">
          <el-input v-model.number="informationForm.no" />
        </el-form-item>
        <!-- 栏目描述 -->
        <el-form-item label="栏目介绍" prop="description">
          <el-input
            v-model="informationForm.description"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <!-- 底部操作按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <!-- 提交表单后 调用重置表单的方法 -->
        <el-button type="primary" @click="submitForm">
          确 定
        </el-button>
      </span>
    </el-dialog>

    <!-- 删除的模态框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible2" width="30%">
      <span>是否删除该栏目</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="offinformation()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from '@/utils/request'
export default {
  data() {
    return {
      dialogVisible2: false,
      // 删除行的数据
      IdInformationdelete: 0,
      // 获取到的信息
      informationData: [],
      dialogVisible: false,
      informationForm: {},
      dialogTitle: '新增资讯',
      rules: {
        // 栏目名称
        name: [{ required: true, message: '请输入栏目名称', trigger: 'blur' }],
        // 产品栏目
        id: [{ required: true, message: '请输入序号', trigger: 'blur' }],
        description: [{ required: true, message: '请输入栏目介绍', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getinformationData()
  },
  mounted() {},
  methods: {
    // 获取数据
    async getinformationData() { // 获取整个页面数据
      const res = await get('/category/findAll', null)
      console.log(res)
      this.informationData = res.data
      var max
      for (var i = 0; i < this.informationData.length; i++) {
        // 外层循环一次，就拿arr[i] 和 内层循环arr.legend次的 arr[j] 做对比
        for (var j = i; j < this.informationData.length; j++) {
          if (this.informationData[i].no > this.informationData[j].no) {
            // 如果arr[j]大就把此时的值赋值给最大值变量max
            max = this.informationData[j]
            this.informationData[j] = this.informationData[i]
            this.informationData[i] = max
          }
        }
      }
      console.log(this.informationData)
    },
    addInformation() {
      this.dialogVisible = true
    },
    dialogClose() {
      this.dialogVisible = false
      this.informationForm={}
    },
    submitForm() {
      this.$refs['information_form'].validate(async(valid) => {
        if (valid) {
          // 发送表单验证的请求 提交表单对象
          const res = await post('/category/saveOrUpdate', this.informationForm)
          // 给用户一个成功的提示
          console.log(res)
          if (res.status == 200) {
            this.$message({
              message: res.message,
              type: 'success'
            })
            // 重新刷新页面的数据
            this.getinformationData()
            // 调用关闭模态框的方法
            this.dialogClose()
          } else {
            this.$message.error(res.message)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    editInformation(row) {
      this.informationForm = { ...row }
      // 预览图片 地址赋值
      // 设置模态框的标题
      this.dialogTitle = '编辑栏目'
      // 模态框显示出来
      this.dialogVisible = true
    },
    async offinformation() {
      const data = {
        id: this.IdInformationdelete
      }
      const res = await get('/category/deleteById', data)
      this.dialogVisible2 = false
      if (res.status == 200) {
        this.$message({
          message: res.message,
          type: 'success'
        })
        // 重新刷新页面的数据
        this.getinformationData()
      } else {
        this.$message.error(res.message)
      }
    }
  }
}
</script>
<style scoped>
</style>
